package gsm.cable.server.module.user;

import gsm.cable.server.datastore.metadata.Kinds;
import gsm.cable.server.module.Address;

import javax.servlet.http.HttpSession;

import com.google.appengine.repackaged.org.json.JSONArray;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;

/**
 * A JSONObject of this class will be maintain in the session.
 * 
 * @author acer
 * 
 */
public class User extends AbsUser {

	private String password_verification_code;

	private String user_verification_code;

	private boolean isVerified = false;

	private HttpSession session;

	public User() {
	}

	public User(JSONObject user, HttpSession session) {
		this(user);
		this.session = session;
	}

	public User(JSONObject user) {
		try {
			setName(user.getString(Kinds.User.NAME));
			setFatherName(user.optString(Kinds.User.FATHER_NAME, null));
			setPassword(user.getString(Kinds.User.PASSWORD));
			setMobileNo(user.optString(Kinds.User.MOBILE_NO));
			this.setUserVerificationCode(user.optString(Kinds.User.USER_VERIFICATION_CODE));
			this.isVerified = user.optBoolean(Kinds.User.IS_VERIFIED);

			JSONArray rolesArray = user.getJSONArray(Kinds.User.ROLES);
			int length = rolesArray == null ? 0 : rolesArray.length();
			if (length == 0) {
				throw new RuntimeException("Problem while initilizing user from session. No role has been specified for the user");
			}
			String[] roles = new String[length];
			for (int roleIndex = 0; roleIndex < length; roleIndex++) {
				roles[roleIndex] = rolesArray.getString(roleIndex);
			}
			setRoles(roles);
			if (user.has(Kinds.ADDRESS)) {
				setAddress(new Address(user.getJSONObject(Kinds.ADDRESS)));
			}
			password_verification_code = user.optString(Kinds.User.PASSWORD_VERIFICATION_CODE);
		} catch (JSONException e) {
			throw new RuntimeException("Problem while initilizing user from session. Main cause is [ " + e.getMessage() + " ]");
		}
	}

	public boolean isUserVerified() {
		return isVerified;
	}

	public JSONObject toJSON() {
		JSONObject user = new JSONObject();
		try {
			user.put(Kinds.TABLE, Kinds.USERS);
			user.put(Kinds.User.EMAIL_ID, getEmailId());
			user.put(Kinds.User.PASSWORD, getPassword());
			user.put(Kinds.User.ROLES, new JSONArray(getRoles()));
			if (!isVerified) {
				user.put(Kinds.User.IS_VERIFIED, false);
				user.put(Kinds.User.USER_VERIFICATION_CODE, getUserVerificationCode());
			}

			String mobileNo = getMobileNo();
			if (mobileNo != null) {
				user.put(Kinds.User.MOBILE_NO, mobileNo);
			}

			Address addreess = getAddreess();
			if (addreess != null) {
				user.put(Kinds.ADDRESS, addreess.toJSON());
			}
			if (password_verification_code != null && password_verification_code.trim().length() > 0) {
				user.put(Kinds.User.PASSWORD_VERIFICATION_CODE, password_verification_code);
			}

		} catch (Exception e) {
			throw new RuntimeException("Problem while converting User object to JSONObject. Main cause is : " + e.getMessage());
		}
		return user;
	}

	public void setUserVerificationCode(String user_verification_code) {
		this.user_verification_code = user_verification_code;
	}

	public String getUserVerificationCode() {
		return user_verification_code;
	}

	public HttpSession getSession() {
		return session;
	}
}
